// packageJ/new_distribution/red_packet/index.js
const app = getApp();
import Dialog from "../../../dist/dialog/dialog";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current_page: 1,
    last_page: 1,

    active: 0,
    show: false,
    list: [],

    red_packet_amount: 0,
    video_list: [],
    amount: 0,
    today_receive: 0,
    is_rest: 0,
    language: "",
    show_cover: false,
    vid: "",

    redpack_video_state: 1,
    imgPath: app.globalData.imgPath,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let language = wx.getStorageSync("langIndex");
    this.setData({
      language: language.en,
    });
    if (options.vid) {
      this.setData({
        vid: options.vid
      });
    }
    this.getData();
    this.getList(1);

  },
  getList(page = 1) {
    app._getNetWork({
      url: app.getNetAddresss("plugin.commission-performance.frontend.redpack.getList"),
      data: {
        is_sub_page: page
      },
      success: resdata => {
        let {
          result,
          msg
        } = resdata.data;
        if (result !== 1) {
          return app.tips(msg);
        }

        let data = resdata.data.data.list;
        this.data.current_page = data.current_page;
        this.data.last_page = data.last_page;

        if (page == 1) {
          this.setData({
            list: data.data
          })
        } else {
          let _list = this.data.list.concat(data.data)
          this.setData({
            list: _list
          })
        }
        console.log(this.data.current_page, this.data.last_page)
      }
    })
  },
  getData() {
    app._getNetWork({
      url: app.getNetAddresss("plugin.commission-performance.frontend.redpack.index"),
      success: resdata => {
        let {
          result,
          msg,
          data
        } = resdata.data;
        if (result !== 1) {
          return app.tips(msg);
        }
        this.setData({
          redpack_video_state: data.redpack_video_state,
          video_list: data.video_list,
          today_receive: data.today_receive,
          amount: data.amount,
          is_rest: data.is_rest
        })
        if (this.data.vid && data.is_rest == 1 && data.today_receive == 0) {
          this.setData({
            show_cover: true
          })
        }
      }
    })

  },
  confirm() {
    let json = {};
    if (this.data.redpack_video_state == 1) {
      json.video_id = this.data.vid
    }
    app._getNetWork({
      url: app.getNetAddresss("plugin.commission-performance.frontend.redpack.receive"),
      data: json,
      success: resdata => {
        let {
          result,
          msg,
          data
        } = resdata.data;
        if (result == 1) {
          this.setData({
            red_packet_amount: data.amount,
            show: true
          })
          this.getData();
        } else {
          app.tips(msg);
        }
        this.setData({
          show_cover: false
        })
      }
    })



  },
  submit() {
    if(this.data.today_receive==1){
      return;
    }
    app._getNetWork({
      url: app.getNetAddresss("plugin.commission-performance.frontend.redpack.index"),
      success: resdata => {
        let {
          result,
          msg,
          data
        } = resdata.data;
        if (result !== 1) {
          return app.tips(msg);
        }
        if (data.redpack_video_state == 0) {
          this.setData({
            show_cover: true
          })
          return;
        }
        let i = Math.floor(Math.random() * data.video_list.length)

        Dialog.confirm({
            title: '观看视频领取红包',
            confirmButtonText: "去观看",
            cancelButtonColor: "#7E7E7E"
          })
          .then(() => {

            wx.navigateTo({
              url: '/packageC/video_goods/VideoDetail/VideoDetail?vid=' + data.video_list[i].id + '&plugin_type=new_distribution'
            })


            // on confirm
          })
          .catch(() => {
            // on cancel
          });

      }
    })





  },
  onClickHide() {
    this.setData({
      show: false
    });

    this.getList()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.current_page >= this.data.last_page) {
      return;
    }
    let page = this.data.current_page + 1;
    this.getList(page)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})